package com.wy.authority.controller;

import com.wy.authority.entity.Dept;
import com.wy.authority.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 忘忧
 * @Date Created in 2021/1/11 15:12
 * @Description:
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;


    @GetMapping("findall")
    public List<Map<String, Object>> tree() {
        //查询出所有的一级菜单【pid=0为一级菜单】
        List<Dept> deptMenu = deptService.find2();
        List<Map<String, Object>> list = new ArrayList<>();
        if (deptMenu.size() > 0) {
            for (Dept dept : deptMenu) {
                Map<String, Object> map = new LinkedHashMap<>();
                if (dept.getStatus().equals("0")) {
                    map.put("status", "正常");
                } else {
                    map.put("status", "停用");
                }
                map.put("id", dept.getDept_id());
                map.put("dept_id", dept.getDept_id());
                map.put("parent_id", dept.getParent_id());
                map.put("ordernum", dept.getOrder_num());
                map.put("leader", dept.getLeader());
                map.put("phone", dept.getPhone());
                map.put("email", dept.getEmail());
                map.put("dept_name", dept.getDept_name());
                map.put("create_time", dept.getCreate_time());
                map.put("children", getChildren(dept.getDept_id()));
                list.add(map);
            }
        }
        return list;
    }


    /**
     * 递归
     *
     * @param deptid
     * @return
     */
    public List<Object>getChildren(Integer deptid){
        List<Object> list = new ArrayList<>();
        List<Dept> deptMenu =deptService.find3(deptid);
        for (Dept dept:deptMenu){
            Map<String,Object>map = new LinkedHashMap<>();
            if (dept.getStatus().equals("0")) {
                map.put("status", "正常");
            } else {
                map.put("status", "停用");
            }
            map.put("id", dept.getDept_id());
            map.put("dept_id", dept.getDept_id());
            map.put("parent_id", dept.getParent_id());
            map.put("order_num", dept.getOrder_num());
            map.put("leader", dept.getLeader());
            map.put("phone", dept.getPhone());
            map.put("email", dept.getEmail());
            map.put("dept_name", dept.getDept_name());
            map.put("create_time", dept.getCreate_time());
            map.put("children", getChildren(dept.getDept_id()));
            list.add(map);
        }
        return list;
    }

    //删除
    @DeleteMapping("/delete/{dept_id}")
    public void delete(@PathVariable("dept_id") Integer dept_id){
        deptService.delete(dept_id);
    }
}